
import moment from "moment";
//默认时间
export const defaultTimeFn = (dateTime) => {
    let Time = {}
    //不传参则为当天早上8点到晚上8点
    if (dateTime == undefined) {
        let startTime = new Date();
        let endTime = new Date();
        startTime.setHours(8, 30, 0);
        endTime.setHours(20, 30, 0);
        Time.startTime = moment(startTime).format("yyyy-MM-DD HH:mm:ss");
        Time.endTime = moment(endTime).format("yyyy-MM-DD HH:mm:ss");
    } else {
        Time.startTime = moment(dateTime[0]).format(
            "yyyy-MM-DD HH:mm:ss"
        );
        Time.endTime = moment(dateTime[1]).format(
            "yyyy-MM-DD HH:mm:ss"
        );
    }
    return Time

}
//默认7天
export const defaultDay7 = (dateTime) => {
    const [startDate, endDate] = dateTime
    //求开始结束时间间隔的天数
    const dayDiff = getDayDiff(dateTime)
    //小于7天
    if (dayDiff < 7) {
        // console.log(moment(newStartTime).format("yyyy-MM-DD HH mm:ss"), moment(endDate).format("yyyy-MM-DD HH:mm:ss"), "小于7天")
        const newStartTime = new Date()
        newStartTime.setTime(new Date(dateTime[1]).getTime() - 3600 * 1000 * 24 * 7)
        newStartTime.setHours(0, 0, 0)
        return [moment(newStartTime).format("yyyy-MM-DD HH:mm:ss"), moment(dateTime[1]).format("yyyy-MM-DD HH:mm:ss")]
    }
    //大于等于7天
    else {
        // console.log(moment(startDate).format("yyyy-MM-DD HH mm:ss"), moment(endDate).format("yyyy-MM-DD HH:mm:ss"), "大于等于7天")
        return [moment(startDate).format("yyyy-MM-DD HH:mm:ss"), moment(endDate).format("yyyy-MM-DD HH:mm:ss")]
    }
}
//默认7周
export const defaultWeek7 = (dateTime) => {
    const [startDate, endDate] = dateTime
    const startDate1 = new Date(startDate);
    const endDate1 = new Date(endDate);
    const dayDiff = getDayDiff(dateTime)//开始结束间隔天数

    let Num = 0//间隔周数初始值
    let startMonday = new Date(startDate1.getTime()).getDay()
    let endMonday = new Date(endDate1.getTime()).getDay()
    //开始date为星期天或星期一
    if (startMonday == 0 || startMonday == 1) {
        Num++
    }
    //开始date不为星期天且不为星期一
    if (startMonday != 1 && startMonday != 0) {
        //结束为星期天或星期一
        if (endMonday == 1 || endMonday == 0) {
            Num++
        }
    }
    //根据开始结束间隔天数统计间隔周数
    for (let i = 0; i < dayDiff; i++) {
        let whatDay = new Date(endDate1.getTime() - (i) * 24 * 60 * 60 * 1000).getDay()
        if (whatDay == 1) Num++
    }
    //根据Num返回数据
    if (Num > 7) {
        return [moment(startDate).format("yyyy-MM-DD HH:mm:ss"), moment(endDate).format("yyyy-MM-DD HH:mm:ss")]
    } else {
        //小于7，手动补足7周
        const newStartTime = new Date()
        let Time = endDate1
        let addNum = 0;
        while (addNum < 7) {
            let returnInfo = Fn(addNum, Time)
            addNum = returnInfo.addNum
            Time = returnInfo.newDate
        }
        newStartTime.setTime(new Date(Time).getTime() + 60 * 60 * 24 * 1000)
        newStartTime.setHours(0, 0, 0);
        // console.log(addNum, moment(newStartTime).format("yyyy-MM-DD HH:mm:ss"), "addNum");
        return [moment(newStartTime).format("yyyy-MM-DD HH:mm:ss"), moment(endDate).format("yyyy-MM-DD HH:mm:ss")]

    }

}
function Fn(addNum, endDate1) {
    let whatDay = new Date(endDate1.getTime()).getDay()
    if (whatDay == 1) addNum++
    let newDate = new Date(endDate1.getTime() - 24 * 60 * 60 * 1000)
    return { addNum, newDate }
}
//求开始结束时间间隔的天数
export const getDayDiff = (dateArr) => {
    const [startDate, endDate] = dateArr;
    const startStamp = Math.floor(new Date(startDate).getTime() / 1000);
    const endStamp = Math.floor(new Date(endDate).getTime() / 1000);
    const timeDiff = endStamp - startStamp;
    return Math.floor(timeDiff / 86400)
}
//************************************** */
//判断时段是否小于30分钟
export const minFn = (startTime, endTime) => {
    let startHous = startTime.split(":")[0];
    let startMin = startTime.split(":")[1];
    let endHous = endTime.split(":")[0];
    let endMin = endTime.split(":")[1];
    return (endHous - startHous) * 60 + (endMin - startMin);
}

